//
//  AuthenticationViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/5.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "AuthenticationViewController.h"
#import "MineViewModel.h"

@interface AuthenticationViewController ()
@property (weak, nonatomic) IBOutlet UITextField *authenNane;
@property (weak, nonatomic) IBOutlet UITextField *authenCardNumber;
@property (weak, nonatomic) IBOutlet UIButton *authClick;
@property (weak, nonatomic) IBOutlet UILabel *userName;

@property (nonatomic,strong) MineViewModel * mineViewModel;

@end

@implementation AuthenticationViewController

-(MineViewModel *) mineViewModel{
    if (!_mineViewModel) {
        _mineViewModel = [[MineViewModel alloc] init];
    }
    return _mineViewModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationItem setTitle:@"身份验证"];
    [self.view setBackgroundColor:DEF_COLOR_F0F0F0];
    
    [self.userName setText:[NSString stringWithFormat:@"用户名: %@",UserInfoShare.userCenterModel.userName]];
    
    [[self.authClick rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        if ([self anthenIfelse]) {
            
            [self anthenNetWork];
        }
    }];

}

-(void) anthenNetWork{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.mineViewModel bandRealInfoRequestParameterName:self.authenNane.text card:self.authenCardNumber.text] subscribeNext:^(id x) {
        
        if ([x[@"errorcode"] integerValue] == 0) {
            
            
            [MXUtility showTips:@"绑定成功～"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        
        [MXUtility hideMBProgress:self.view];
    }error:^(NSError *error) {
        [MXUtility hideMBProgress:self.view];
    }];
}
-(BOOL) anthenIfelse{
    
    if ([MXUtility NSNULLString:self.authenNane.text]) {
        [MXUtility showTips:@"姓名不能为空"];
        return false;
    }
    if ([MXUtility NSNULLString:self.authenCardNumber.text]) {
        [MXUtility showTips:@"身份证不能为空"];
        return false;
    }
    if (![MXUtility judgeIdentityCardNo:self.authenCardNumber.text]) {
        [MXUtility showTips:@"请输入正确的身份证号"];
        return false;
    }
    
    return true;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
